﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace ZG_member
{
    public partial class SalesPersonAdd : Form
    {
        int id = 0;
        SalesStaffList st;
        public SalesPersonAdd(SalesStaffList ss)
        {
            InitializeComponent();
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
            st = ss;
            this.txtSalesId.ReadOnly = false;
        }

        public SalesPersonAdd(int ids,SalesStaffList ss)
        {
            InitializeComponent();
            id = ids;
            this.Text = "销售修改";
            string path = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
            st = ss;
            this.txtSalesId.ReadOnly = true;
        }


        private void SalesPersonAdd_Load(object sender, EventArgs e)
        {
            timer1.Start();
            if (id != 0)
            {
                ZG_member.BLL.SalesStaffList sale = new ZG_member.BLL.SalesStaffList();
                ZG_member.Model.SalesStaffList saleMode = new ZG_member.Model.SalesStaffList();
                saleMode= sale.GetModel(id);
                this.txtName.Text = saleMode.Name;
                this.txtSalesId.Text = saleMode.SalesPersonId;
                this.txtMoney.Text = saleMode.Money.ToString();
                this.txtBirthDate.Text = saleMode.BirthDate.ToString();
                if (saleMode.Sex == "男")
                {
                    this.radioButton1.Checked = true;
                }
                else
                {
                    this.radioButton2.Checked = true;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }
        int flag = 0;
        private void SalesPersonAdd_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sex="";
            string name = this.txtName.Text;
            string money = this.txtMoney.Text;
            string birthdate = this.txtBirthDate.Text;
            string saleId = this.txtSalesId.Text;
            ZG_member.BLL.SalesStaffList sale = new ZG_member.BLL.SalesStaffList();
            ZG_member.Model.SalesStaffList saleMode = new Model.SalesStaffList();
            if (radioButton1.Checked == true)
            {
                sex= radioButton1.Text;
            }
            if (radioButton2.Checked == true)
            {
                sex = radioButton2.Text;
            }
            if (sex == "")
            {
                MessageBox.Show("性别不能为空");
                return;
            }
            if (saleId == "")
            {
                MessageBox.Show("销售编号不能为空");
                return;
            }
            if (money == "")
            {
                money = "0";
            }

            if (this.Text == "销售修改")
            {
               saleMode=sale.GetModel(id);
               saleMode.Name = name;
               saleMode.BirthDate =Convert.ToDateTime(birthdate);
               saleMode.Sex = sex;
               saleMode.Money = Convert.ToDouble(money);
               sale.Update(saleMode);
               MessageBox.Show("修改成功");
               this.Close();
            }
            else
            {
                try
                {
                    saleMode.Name = name;
                    saleMode.Money = Convert.ToDouble(money);
                    saleMode.BirthDate = Convert.ToDateTime(birthdate);
                    saleMode.SalesPersonId = saleId;
                    saleMode.Sex = sex;
                    sale.add(saleMode);
                    MessageBox.Show("注册成功");
                }
                catch (Exception)
                {

                    MessageBox.Show("此编号已注册，请重新输入！");
                    return;
                }

            }
            st.DgvSales.DataSource = sale.GetListWhere("").Tables[0];
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
